using System;
using MyHomeworkWebAPi.Models.ef;

namespace MyHomeworkWebAPi.Models.DTOs
{
    public class ProductDTO
    {
        /// <summary>
        /// 商品ID
        /// </summary>
        public Guid ProductId { get; set; }

        /// <summary>
        /// 发布者ID
        /// </summary>
        public Guid UserId { get; set; }

        /// <summary>
        /// 发布者昵称
        /// </summary>
        public string UserNickname { get; set; }

        /// <summary>
        /// 商品标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 价格
        /// </summary>
        public decimal Price { get; set; }

        /// <summary>
        /// 商品类别ID
        /// </summary>
        public Guid CategoryId { get; set; }

        /// <summary>
        /// 商品类别名称
        /// </summary>
        public string CategoryName { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 商品图片（Base64格式）
        /// </summary>
        public string ImageBase64 { get; set; }

        // Include only necessary user information
        public UserDTO? User { get; set; }

        public static ProductDTO? FromProduct(MyHomeworkWebAPi.Models.ef.Product? product)
        {
            if (product == null) return null;

            return new ProductDTO
            {
                ProductId = product.ProductId,
                UserId = product.UserId,
                UserNickname = product.User?.Nickname,
                Title = product.Title,
                Price = product.Price,
                CategoryId = product.CategoryId,
                CategoryName = product.Category?.Name,
                Status = product.Status,
                Description = product.Description,
                ImageBase64 = product.Image != null ? Convert.ToBase64String(product.Image) : null,
                User = product.User != null ? UserDTO.FromUser(product.User) : null
            };
        }
    }
} 